﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;

using Kooboo.Commerce.AddIn;

namespace Kooboo.Commerce.Payments.PayPal
{
    [Export(typeof(IPaymentMethod))]
    public class PaymentMethod : IPaymentMethod
    {
        public string Name
        {
            get { return "Paypal"; }
        }

        public string HeadLine
        {
            get { return "Paypal"; }
        }

        #region IPaymentMethod Members

        IPaymentMethodViews _Views;
        public IPaymentMethodViews Views
        {
            get
            {
                if (this._Views == null)
                {
                    this._Views = new PaymentMethodViews();
                }
                return this._Views;
            }
        }

        public void TryToPay(IOrder order, String currencyCode)
        {
            var settings = Services.FindSettings();
            //test.SetCredentials(settings.APIUsername, settings.APIPassword, settings.APISignature);

            var ap = new AdaptivePayment(new Authentication
            {
                X_PAYPAL_SECURITY_USERID = settings.APIUsername,
                X_PAYPAL_SECURITY_PASSWORD = settings.APIPassword,
                X_PAYPAL_SECURITY_SIGNATURE = settings.APISignature
            });

            ap.Pay(new Payload
            {
                X_PAYPAL_REQUEST_DATA_FORMAT = PayloadDataFormat.NV,
                X_PAYPAL_RESPONSE_DATA_FORMAT = PayloadDataFormat.NV
            }, new ClientIndentification(),
            settings.GoLive, settings.MerchantAccount, order.Total, currencyCode, order.Id, ThankYouPageUrl);

            /*
            NVPAPICaller test = new NVPAPICaller();
            string retMsg = "";
            string token = "";

         

            var baseUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + (HttpContext.Current.Request.Url.Port == 80 ? "" : ":" + HttpContext.Current.Request.Url.Port.ToString()) + "/Commerce";

            
            if (order.Total != decimal.Zero)
            {
                string[] amts = order.Total.ToString().Split(new char[] { '.' });
                string amt = amts[0] + "." + amts[1].Substring(0, 2);

                bool ret = test.ShortcutExpressCheckout(amt,order.Id.ToString(), ref token, ref retMsg);
                if (ret)
                {
                    PaypalOrderTokenDataContext orderTokenDataContext = new PaypalOrderTokenDataContext();
                    Payment_Paypal_OrderToken obj = new Payment_Paypal_OrderToken();
                    obj.OrderId = order.Id;
                    obj.Token = token;
                    orderTokenDataContext.Payment_Paypal_OrderToken.InsertOnSubmit(obj);
                    orderTokenDataContext.SubmitChanges();
                    //string str=test.HttpCall(retMsg);
                    HttpContext.Current.Response.Redirect(retMsg);
                }
                else
                {
                    HttpContext.Current.Response.Redirect(baseUrl + "/PayPal/APIError?" + retMsg);
                }
            }
            else
            {
                HttpContext.Current.Response.Redirect(baseUrl+"/PayPal/APIError?ErrorCode=AmtMissing");
            }
             * */
        }

        #endregion

        public string ThankYouPageUrl
        {
            get;
            set;
        }
    }
}